<template>
  <div class="container">
    <el-dialog
      width="500px"
      title="员工导入"
      :visible="showExcelDialog"
      @close="$emit('update:showExcelDialog', false)"
    >
      <!-- class="excel-upload-input" 将该input隐藏 -->
      <el-row type="flex" justify="center">
        <div class="upload-excel">
          <input
            ref="excelupload"
            class="excel-upload-input"
            type="file"
            accept=".xlsx, .xls"
            @change="uploadChange"
          >
          <div class="drop">
            <i class="el-icon-upload" />
            <el-button type="text" @click="getExportTemplate">下载导入模板</el-button>
            <span>将文件拖到此处或
              <el-button type="text" @click="excelupload">点击上传</el-button>
            </span>
          </div>
        </div>
      </el-row>
      <el-row type="flex" justify="end">
        <!-- update:props属性名，值 直接修改 .sync修饰符的属性值 -->
        <el-button size="mini" type="primary" @click="$emit('update:showExcelDialog', false)">取消</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getExportTemplateApi, uploadExcelApi } from '@/api/employee'
import FileSaver from 'file-saver'

export default {
  name: '',
  props: {
    showExcelDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {

    }
  },
  methods: {
    async getExportTemplate() {
      const BlobFiles = await getExportTemplateApi()
      FileSaver.saveAs(BlobFiles, '员工导入模版.xlsx')
      this.$message.success('下载员工导入模版成功~~~')
    },
    // 这个操作是点击 class="excel-upload-input" 的input（隔山打牛）
    excelupload() {
      this.$refs.excelupload.click()
    },
    // 这个方法是拿到上传文件的内容
    async uploadChange(event) {
      console.log(event.target.files)
      // 1. 获取文件的数组对象
      const files = event.target.files
      // 2. 判断
      if (files.length > 0) {
        // 2.1 取出数组中第一个文件
        const data = files[0]
        // 2.2 创建实例
        const fd = new FormData()
        // 3.3 将文件参数加入到formData中
        fd.append('file', data)
        // 3.4 请求数据
        try {
          await uploadExcelApi(fd)
          this.$message.success('上传成功')
          this.$emit('update:showExcelDialog', false)
          this.$emit('uploadData')
        } catch (error) {
          this.$message.error('上传失败')
          this.$refs.excelupload.values = ''
        } finally {
          // 不论成功或者失败都会执行finally
          this.$refs.excelupload.values = ''
        }
      }
    }
  }

}
</script>

<style scoped lang="scss">
.upload-excel {
    display: flex;
    justify-content: center;
    margin: 20px;
    width: 360px;
    height: 180px;
    align-items: center;
    color: #697086;
    .excel-upload-input {
      display: none;
      z-index: -9999;
    }
    .btn-upload,
      .drop {
      border: 1px dashed #dcdfe6;
      width: 100%;
      height: 100%;
      text-align: center;
      line-height: 160px;
      border-radius: 8px;
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    .drop {
      line-height: 40px;
      color: #bbb;
      i {
        font-size: 60px;
        display: block;
        color: #c0c4cc;
      }
    }
  }
</style>
